package com.wzc.bigevent.controller;

import com.wzc.bigevent.pojo.Article;
import com.wzc.bigevent.pojo.PageBean;
import com.wzc.bigevent.pojo.Result;
import com.wzc.bigevent.service.ArticleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 添加文章
     * 请求地址：/article
     * 请求方式：POST
     * 请求参数：article对象
     *
     * @return
     */
    @PostMapping()
    public Result addArticle(@RequestBody @Validated Article article) {
        // 添加文章
        articleService.addArticle(article);
        return Result.success();
    }


    /**
     * 根据id获取文章详情
     * 请求地址：/article/detail
     * 请求方式：GET
     * 请求参数：id
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> getArtivleDetail(@RequestParam Integer id) {
        // 获取文章详情
        return Result.success(articleService.getArticleDetail(id));
    }


    /**
     * 更新文章
     * 请求地址：/article
     * 请求方式：PUT
     * 请求参数：article对象
     *
     * @param article
     * @return
     */
    @PutMapping()
    public Result updateArticle(@RequestBody @Validated Article article) {
        // 更新文章
        articleService.updateArticle(article);
        return Result.success();
    }


    /**
     * 删除文章
     * 请求地址：/article
     * 请求方式：DELETE
     * 请求参数：id
     *
     * @param id
     * @return
     */
    @DeleteMapping()
    public Result deleteArticle(@RequestParam @Validated Integer id) {
        articleService.deleteArticle(id);
        return Result.success();
    }


    /**
     * 获取文章列表 分页对象
     * @return
     */
    @GetMapping()
    public Result<PageBean<Article>> getArticleList(Integer pageNum,
                                                    Integer pageSize,
                                                    @RequestParam (required = false) String state,
                                                    @RequestParam (required = false) Integer categoryId)  {
        // 获取文章列表
        return Result.success(articleService.getArticleList(pageNum, pageSize, state, categoryId));
    }
}

